<?php
/************************************************************
 * Copyright (C), 1993-~, Dacelve. Tech., Ltd.
 * FileName : Redis.php
 * Author   : Lizhijian
 * Version  : 1.0
 * Date     : 2018/2/7 17:18
 * Description   : 阿里云Redis 应用类
 * Main Function :
 * History  :
 * <author>    <time>    <version >    <desc>
 * Lizhijian   2018/2/8   1.0          init
 ***********************************************************/
namespace app;
/**
 * redis应用类
 * Class Redis
 * @package app\common\library
 * Fashion:
 */
class Redis
{

    /**
     * @var \Redis
     */
    protected static $redis;
    public static $dbindex = 0;//操作的数据库下标

    /**
     * Redis constructor.
     */
    public function __construct()
    {
        return self::init();
    }

    /**
     * @name redis链接
     * @Description
     * @remark
     * @author Lizhijian
     * @return \Redis
     * @example
     */
    public static function init()
    {
        self::$redis = new \Redis;
        self::$redis->connect('120.79.209.186', 6379);
        self::$redis->auth('myRedis123');
        self::$redis->select(self::$dbindex);
        return self::$redis;
    }

    /**
     * @name 设置key
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $val
     * @param int $time
     * @example
     */
    public static function set($key, $val, $time = 0)
    {
        self::init();
        $time > 0 ? self::$redis->setex($key, $time, $val) : self::$redis->set($key, $val);
    }

    /**
     * @name 取key
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @return bool|string
     * @example
     */
    public static function get($key)
    {
        self::init();
        return self::$redis->get($key);
    }

    /**
     * @name 删除key
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @return int
     * @example
     */
    public static function del($key)
    {
        self::init();
        return self::$redis->del($key);
    }

    /**
     * @name 搜索key
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @return array
     * @example
     */
    public static function keys($key)
    {
        self::init();
        return self::$redis->keys($key);
    }

    /**
     * @name 设置key
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $val
     * @param int $time
     * @example
     */
    public static function sSet($key, $val, $time = 0)
    {
        $redis = new \Redis;
        $redis->connect(config('cache.host'), config('cache.port'));
        $redis->auth(config('cache.password'));
        $redis->select(1);
        $redis->set($key, $val, $time);
    }

    /**
     * @name redis队列 入队
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $value1
     * @return int
     * @example
     */
    public static function lPush($key, $value1)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->lPush($key, $value1);
    }

    /**
     * @name redis队列 入队
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $value1
     * @return int
     * @example
     */
    public static function rPush($key, $value1)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->rPush($key, $value1);
    }

    /**
     * @name redis队列 出队
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @return string
     * @example
     */
    public static function rPop($key)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->rPop($key);
    }

    /**
     * @name redis队列 出队
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @return string
     * @example
     */
    public static function lPop($key)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->lPop($key);
    }

    /**
     * @name 添加集合
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $value1
     * @return int
     * @example
     */
    public static function sAdd($key, $value1)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->sAdd($key, $value1);
    }

    /**
     * @name 移除集合元素
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $member1
     * @return int
     * @example
     */
    public static function sRem($key, $member1)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->sRem($key, $member1);
    }

    /**
     * @name 获取集合所有成员
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @return array
     * @example
     */
    public static function sMembers($key)
    {
        self::init();
        self::$redis->select(1);
        return self::$redis->sMembers($key);
    }

    /**
     * @name 设置哈希表内容
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param $hashKey
     * @param $value
     * @param int $time 哈希表的全局时间[设置单个key时注意]
     * @example hashSet('user', 'info', array('name' => 'Joe', 'salary' => 8000));
     */
    public static function hashSet($key, $hashKey, $value, $time = 0){
        self::init();
        self::$redis->hSet($key, $hashKey, $value);
        if($time > 0) self::$redis->expire($key, $time);
    }

    /**
     * @name 获取哈希表内容
     * @Description
     * @remark
     * @author Lizhijian
     * @param $key
     * @param string $hashKeys 无hashKeys时取所有域的值
     * @return array|string
     * @example hashGet('user', 'info');  or  hashGet('user');
     */
    public static function hashGet($key, $hashKeys = ''){
        self::init();
        if($hashKeys){
            return self::$redis->hGet($key, $hashKeys);
        }else{
            return self::$redis->hGetAll($key);
        }
    }
}